package com.ruoyi.adapayMember.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.huifu.adapay.core.exception.BaseAdaPayException;
import com.huifu.adapay.model.AdapayCommon;
import com.huifu.adapay.model.Member;
import com.huifu.adapay.model.SettleAccount;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.adapayMember.mapper.AdapayMemberMapper;
import com.ruoyi.adapayMember.domain.AdapayMember;
import com.ruoyi.adapayMember.service.IAdapayMemberService;

/**
 * 汇付分账用户Service业务层处理
 *
 * @author ch
 * @date 2022-02-22
 */
@Service
public class AdapayMemberServiceImpl implements IAdapayMemberService
{
    @Autowired
    private AdapayMemberMapper adapayMemberMapper;
    @Autowired
    private ISysConfigService configService;
    /**
     * 查询汇付分账用户
     *
     * @param id 汇付分账用户主键
     * @return 汇付分账用户
     */
    @Override
    public AdapayMember selectAdapayMemberById(Long id)
    {
        return adapayMemberMapper.selectAdapayMemberById(id);
    }

    /**
     * 查询汇付分账用户列表
     *
     * @param adapayMember 汇付分账用户
     * @return 汇付分账用户
     */
    @Override
    public List<AdapayMember> selectAdapayMemberList(AdapayMember adapayMember)
    {
        return adapayMemberMapper.selectAdapayMemberList(adapayMember);
    }

    /**
     * 新增汇付分账用户
     *
     * @param adapayMember 汇付分账用户
     * @return 结果
     */
    @Override
    public int insertAdapayMember(AdapayMember adapayMember)
    {
        adapayMember.setCreateTime(DateUtils.getNowDate());
        return adapayMemberMapper.insertAdapayMember(adapayMember);
    }

    /**
     * 修改汇付分账用户
     *
     * @param adapayMember 汇付分账用户
     * @return 结果
     */
    @Override
    public int updateAdapayMember(AdapayMember adapayMember)
    {
        adapayMember.setUpdateTime(DateUtils.getNowDate());
        return adapayMemberMapper.updateAdapayMember(adapayMember);
    }

    /**
     * 批量删除汇付分账用户
     *
     * @param ids 需要删除的汇付分账用户主键
     * @return 结果
     */
    @Override
    public int deleteAdapayMemberByIds(Long[] ids)
    {
        return adapayMemberMapper.deleteAdapayMemberByIds(ids);
    }


    /**
     * 批量逻辑删除汇付分账用户
     *
     * @param ids 需要删除的汇付分账用户主键
     * @return 结果
     */
    @Override
    public int logicDeleteAdapayMember(Long[] ids,String delType,Long delUser)
    {
        return adapayMemberMapper.logicDeleteAdapayMember(ids,delType,delUser);
    }

    /**
     * 删除汇付分账用户信息
     *
     * @param id 汇付分账用户主键
     * @return 结果
     */
    @Override
    public int deleteAdapayMemberById(Long id)
    {
        return adapayMemberMapper.deleteAdapayMemberById(id);
    }

    public Map<String, Object> createMember(String member_id, String phone, String  userName, String idCard){
        String app_id=configService.selectConfigByKey("adpay_app_id");
        Map<String, Object> memberParams = new HashMap<String, Object>();
        memberParams.put("adapay_func_code", "members.realname");
        memberParams.put("member_id", member_id);
        memberParams.put("app_id",app_id);
        memberParams.put("tel_no", phone);
        memberParams.put("user_name", userName);
        memberParams.put("cert_type", "00");
        memberParams.put("cert_id", idCard);
        System.out.println("创建用户，请求参数：" + JSON.toJSONString(memberParams));
        Map<String, Object> member = null;
        try {
            member =  AdapayCommon.requestAdapay(memberParams);
            System.out.println("创建用户，返回参数：" + JSON.toJSONString(member));
            System.out.println("=======execute CreateMember end=======");
        } catch (BaseAdaPayException e) {
            e.printStackTrace();
        }


        return member;
    }


}
